package com.bdcom.dialog;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bdcom.comp.CanvasPanel;
import com.bdcom.model.AbstractDevice;
import com.bdcom.model.Device;
import com.bdcom.util.PlannerUtil;
@Component
public class UpdateDevicePropertyDialog extends JDialog implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextField deviceNameField;
	
	private JTextField macAddressField;
	
	private JButton saveButton;
	
	@Autowired
	private CanvasPanel canvasPanel;	

	public UpdateDevicePropertyDialog() {
		super();
		// TODO Auto-generated constructor stub
		
		this.setTitle(PlannerUtil.getString("update device property"));

		this.setSize(new Dimension(300, 200));
		
		this.setResizable(false);

		this.setModal(true);

		JPanel propertyPanel = new JPanel();
		
		// 布局管理器
		GridBagLayout gbl = new GridBagLayout();

		propertyPanel.setLayout(gbl);

		GridBagConstraints gbc = new GridBagConstraints();
		
		gbc.insets=new Insets(5, 0, 0, 10);
		
		gbc.gridx=0;

		propertyPanel.add(new JLabel(PlannerUtil.getString("device name")), gbc);
		
		gbc.gridx=1;
		
		propertyPanel.add(deviceNameField=new JTextField(10), gbc);
		
		gbc.gridx=0;
		
		propertyPanel.add(new JLabel(PlannerUtil.getString("mac address")), gbc);

		gbc.gridx=1;
		
		propertyPanel.add(macAddressField=new JTextField(10), gbc);
		
		gbc.gridx=0;
		
		gbc.gridwidth=2;
		
		gbc.insets=new Insets(20, 0, 0, 10);
		
		propertyPanel.add(saveButton=new JButton(PlannerUtil.getString("save")),gbc);
		
		saveButton.addActionListener(this);
		
		this.add(propertyPanel);
		
		this.setLocationRelativeTo(null);
		
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		
		Device d=(Device) canvasPanel.getSelectedObject();
		
		deviceNameField.setText(d.getDeviceName());
		
		macAddressField.setText(d.getMacAddress());		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String name=deviceNameField.getText();
		
		if(name==null||"".equals(name)){
			return;
		}
		
		String mac=macAddressField.getText();
		
		if(mac==null||"".equals(mac)){
			return;
		}
		
		if(!mac.matches("[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}")){
			
			JOptionPane.showMessageDialog(null,PlannerUtil.getString("please input like this")+":FFFF-FFFF-FFFF");	
			
			return;
		}
		
		((AbstractDevice)canvasPanel.getSelectedObject()).setDeviceName(name);

		((AbstractDevice)canvasPanel.getSelectedObject()).setMacAddress(mac);
		
		canvasPanel.repaint();
		
		canvasPanel.setSelectedObject(canvasPanel.getSelectedObject());
		
		setVisible(false);
	}
	

}
